package com.css.landslide.risk.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.css.exception.CommonResult;
import com.css.exception.GlobalErrorCodeConstants;
import com.css.exception.ServiceException;
import com.css.landslide.risk.entity.LandslideRiskLevel;
import com.css.landslide.risk.param.LandslideParam;
import com.css.landslide.risk.service.LandslideRiskLevelService;
import com.css.landslide.risk.service.LandslideRiskService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author dingling
 * @desc 地震滑坡次生灾害评估
 * @date 2024-09-26 16:55
 **/
@RestController
@Slf4j
@Validated
@RequestMapping("/landslideLevel/risk")
public class LandslideRiskController {


    @Autowired
    private LandslideRiskService landslideRiskService;

    @Autowired
    private LandslideRiskLevelService landslideRiskLevelService;

    /**
     * 计算滑坡局域危险等级
     */
    @PostMapping("/update")
    public CommonResult<String> calculate(@RequestBody LandslideParam landslideRisk) {

        if (StringUtils.isBlank(landslideRisk.getEarthQuakeId()) ){
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        if (StringUtils.isBlank(landslideRisk.getInfluenceVersion()) ){
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        if (StringUtils.isBlank(landslideRisk.getDataSourceKey()) ){
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        landslideRiskService.calculateLandslideRiskManual(landslideRisk);
        return CommonResult.success("计算完成");
    }


    @GetMapping("/list")
    public CommonResult<List<LandslideRiskLevel>> details(@RequestBody LandslideParam landslideRisk){
        Assert.notBlank(landslideRisk.getEarthQuakeId(),"地震id不能为空");

        List<LandslideRiskLevel> riskLevels = landslideRiskLevelService.selectRiskLandslideProvince(landslideRisk);
        return CommonResult.success(riskLevels);
    }


    @GetMapping("/test")
    public CommonResult test(){

        LandslideParam dsEntity = new LandslideParam();
        dsEntity.setDataSourceKey("province_root");
        dsEntity.setEarthQuakeId("1111");
        dsEntity.setInfluenceVersion("");
        List<LandslideRiskLevel> levels = new ArrayList<>();
        for (int i = 0; i < 400000; i++) {
            LandslideRiskLevel level = new LandslideRiskLevel();
            level.setId(IdUtil.simpleUUID());
            level.setEarthQuakeId("1111");
            level.setDzVersion("7");
            level.setDzIntensity("111");
            level.setGeom("0103000020E61000000100000005000000FD2569152E345940816E9BC92026414086FFA9BC3B345940816E9BC92026414086FFA9BC3B3459406EBB197B05264140FD2569152E3459406EBB197B05264140FD2569152E345940816E9BC920264140");
            level.setCreateTime(new Date());
            levels.add(level);
        }

        landslideRiskLevelService.batchInsert(dsEntity, levels);
        return CommonResult.success(1);
    }












}
